﻿// 功能：背景灯控制API
// 描述：
// 作者：温伟鹏
// 日期：2010/5/12 11:19:20

using System;
using System.Runtime.InteropServices;

namespace MeizuSDK.Core
{
    /// <summary>
    /// 背景灯控制API
    /// </summary>
    public class BackLightAPI
    {
        #region DllImport
        /// <summary>
        /// 获取背光等级
        /// </summary>
        /// <param name="pLevel">返回背光等级.(0-255)</param>
        /// <returns></returns>
        [DllImport("PlatformAPIWrapper.dll", EntryPoint = "GetBackLightLevelEx", SetLastError = true)]
        private static extern bool GetBackLightLevel(ref uint pLevel);
        /// <summary>
        /// 获取环境感光开关状态
        /// </summary>
        /// <param name="bOpened">存储环境感光开关状态</param>
        /// <returns></returns>
        [DllImport("PlatformAPIWrapper.dll", EntryPoint = "GetLuxSensorStateEx", SetLastError = true)]
        private static extern bool GetLuxSendorState(ref bool bOpened);
        /// <summary>
        /// 获取系统从IDLE状态到SUSPEND的超时时间 
        /// </summary>
        /// <returns>回timeout值,单位是毫秒。返回-1，表示函数执行失败</returns>
        [DllImport("PlatformAPIWrapper.dll", EntryPoint = "GetSystemIdleTimeoutEx", SetLastError = true)]
        public static extern int GetSystemIdleTimeout();
        /// <summary>
        /// 设置背光等级 
        /// </summary>
        /// <param name="level">将要设置的背光等级(0 - 255). </param>
        /// <returns></returns>
        [DllImport("PlatformAPIWrapper.dll", EntryPoint = "SetBackLightLevelEx", SetLastError = true)]
        private static extern bool SetBackLightLevel(uint level);
        /// <summary>
        /// 设置背光开关状态
        /// </summary>
        /// <param name="bOpened">true打开背光；false，关闭背光</param>
        /// <returns></returns>
        [DllImport("PlatformAPIWrapper.dll", EntryPoint = "SetBackLightStateEx", SetLastError = true)]
        private static extern bool SetBackLightState(bool bOpened);
        /// <summary>
        /// 设置环境感光开关状态
        /// </summary>
        /// <param name="bOpened">true，打开环境感光；false，关闭环境感光</param>
        /// <returns></returns>
        [DllImport("PlatformAPIWrapper.dll", EntryPoint = "SetLuxSensorStateEx", SetLastError = true)]
        private static extern bool SetLuxSensorState(bool bOpened);
        #endregion

        /// <summary>
        /// 设置背光等级
        /// </summary>
        /// <param name="level">将要设置的背光等级(0 - 255).</param>
        /// <returns>true表示设置成功；false表示设置失败</returns>
        public static bool SetBackLightLevel(byte level)
        {
            bool result = false;

            try
            {
                result = SetBackLightLevel((uint)level);

                return result;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 获取背光等级
        /// </summary>
        /// <returns>返回背光等级 0-255</returns>
        public static byte GetBackLighLevel()
        {
            uint level = 0;

            try
            {
                GetBackLightLevel(ref level);
            }
            catch
            {}

            return (byte)level;
        }
        /// <summary>
        /// 打开背光
        /// </summary>
        /// <returns>返回true，表示打开成功；否则表示打开失败</returns>
        public static bool OpenBackLight()
        {
            return SetBackLightState(true);
        }

        /// <summary>
        /// 关闭背光
        /// </summary>
        /// <returns>返回true，表示关闭成功；否则表示关闭失败</returns>
        public static bool CloseBackLight()
        {
            return SetBackLightState(false);
        }
        /// <summary>
        /// 打开环境感光
        /// </summary>
        /// <returns>返回true，表示打开成功；否则表示打开失败</returns>
        public static bool OpenLuxSensor()
        {
            return SetLuxSensorState(true);
        }
        /// <summary>
        /// 判断当前是否打开了环境感光开关
        /// </summary>
        /// <returns></returns>
        public static bool IsLuxSensorOpened()
        {
            bool result = false;

            GetLuxSendorState(ref result);

            return result;
        }
        /// <summary>
        /// 关闭环境感光
        /// </summary>
        public static void CloseLuxSensor()
        {
            if (IsLuxSensorOpened())
                SetLuxSensorState(false);
        }
    }
}
